package com.firegod.servicebase.entity;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table
@Getter
@NoArgsConstructor
public class Course {

    // 课程名 课程号* 课程封面 课程简介 教师 视频 学生 助教 学期 直播地址 圈子

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @ManyToMany
    @Setter
    @JoinTable(name = "student_course",
            joinColumns = @JoinColumn(name = "course_id"),
            inverseJoinColumns = @JoinColumn(name = "student_id"),
            uniqueConstraints = {@UniqueConstraint(columnNames = {"course_id", "student_id"})})
    private List<Student> studentList;

    @Column(nullable = false)
    @Setter
    private String title;

    @Column(nullable = false, unique = true)
    @Setter
    private String courseId;

    @Column(columnDefinition = "text", nullable = false)
    @Setter
    private String description;  // 简介

    @Setter
    private String cover;  // 封面

    @Setter
    private String liveAddress;  // 直播地址

    @Column(nullable = false)
    @Setter
    private String semester;

    @Setter
    private AtomicInteger studentNumber;  // 学生数量

    @Column(nullable = false)
    @Setter
    private String tag;  // 标签

    @Setter
    private Boolean live;  // 是否正在直播

    @ManyToOne
    @Setter
    @JsonIgnore
    private Teacher teacher;

    @ManyToMany
    @JoinTable(name = "course_teacher_assistant")
    @Setter
    private List<Student> teacherAssistantList;  // 助教列表

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "course_chapter")
    @Setter
    private List<Chapter> chapterList;

    @OneToOne(cascade = CascadeType.ALL)
    @Setter
    @JsonIgnore
//    @JoinTable(name = "course_group")
    @JoinColumn(name = "group_id")
    private Group group;

    public Course(Teacher teacher, String courseId, String title) {
        this.teacher = teacher;
        this.courseId = courseId;
        this.title = title;
        this.live = false;
        this.liveAddress = courseId;
        this.studentNumber = new AtomicInteger(0);
    }

    public int getNumOfStudent() {
        if (studentNumber == null) {
            return studentList.size();
        }
        return studentNumber.intValue();
    }

    public JSONObject show() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("courseId", courseId);
        jsonObject.put("cover", cover);
        jsonObject.put("description", description);
        jsonObject.put("stuNum", studentNumber);
        jsonObject.put("userId", teacher.getUserId());
        jsonObject.put("teacherName", teacher.getName());
        jsonObject.put("semester", semester);
        jsonObject.put("title", title);
        jsonObject.put("tag", tag);
        return jsonObject;
    }

    public JSONObject showDetail(boolean join) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("courseId", courseId);
        jsonObject.put("cover", cover);
        jsonObject.put("description", description);
        jsonObject.put("stuNum", studentNumber);
        jsonObject.put("teacherId", teacher.getUserId());
        jsonObject.put("teacherName", teacher.getName());
        jsonObject.put("semester", semester);
        jsonObject.put("title", title);
        jsonObject.put("tag", tag);
        jsonObject.put("groupId", group.getGroupId());
        jsonObject.put("live", live);
        jsonObject.put("liveAddress", liveAddress);
        jsonObject.put("join", join);
        jsonObject.put("chapterList", chapterList.stream().map(Chapter::show).toArray(JSONObject[]::new));
        return jsonObject;
    }

    public JSONObject showChapterList() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("chapterList", chapterList.stream().map(Chapter::show).toArray(JSONObject[]::new));
        return jsonObject;
    }
}
